package com.lmc.base.demo.ai.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.ResourceAccessException;

import java.net.ConnectException;
import java.time.LocalDateTime;

/**
 * @author lmc
 * @Description: TODO
 * @Create 2024-07-26 21:38
 * @version: 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(ConnectException.class)
    public ResponseEntity<String> handleConnectException(ConnectException e) {
        // 这里可以记录日志，处理异常信息，返回自定义的错误信息等
        log.warn("抛出ConnectException,  {}", e);
        log.info(LocalDateTime.now().toString());

        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> handleException(Exception e) {
        // 这里可以记录日志，处理异常信息，返回自定义的错误信息等
        log.warn("抛出Exception,  {}", e);
        log.info(LocalDateTime.now().toString());

        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

    @ExceptionHandler(ResourceAccessException.class)
    public ResponseEntity<String> handleResourceAccessException(ResourceAccessException e) {
        // 这里可以记录日志，处理异常信息，返回自定义的错误信息等
        log.warn("抛出ResourceAccessException,  {}", e.getMessage());
        log.info(LocalDateTime.now().toString());

        return new ResponseEntity<>("An error occurred: " + e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
    }

}
